<?php

namespace app\test628\controller\admin;

use app\test628\model\Users;
use think\Controller;
use think\Request;

class Login extends Controller
{
    public function login(Request $request)
    {
        $data = $request->param();

        $data['password'] = md5(md5($data['password']));


        //根据用户名称查询用户是否存在

        $res = Users::when('username', $data['username'])->when('tel',$data['username'])->find();

        //根据结果可以返回值

        if ($res) {
            if ($res['password'] == $data['password']) {
                return json(['code' => 200, 'msg' => '登录成功!', 'data' => $res]);
            } else {
                return json(['code' => 500, 'msg' => '账号或密码错误!', 'data' => '']);
            }
        } else {
              return json(['code'=>500,'msg'=>'账号或密码错误!','data'=>'']);
        }
    }

    public function getUrl($url)
    {
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);

        dump($output);die();
        return $output;
    }


    /**
     *Method openid
     *user:大飞
     *time:2021/7/2 19:31
     */
    /***
     *  用户授权登陆
     */
    public function getOpenId(Request $request){
        $code = $request->get('code','');
        $apppId = "wx32e831fc5b1760b8";
        $AppSecret = "63e1c2674db78bdd7d4ab995a4de27e6";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$apppId}&secret={$AppSecret}&js_code={$code}&grant_type=authorization_code";

        $res = $this->getUrl($url);
        dump($res);die();
        if(!isset($res['errcode'])){
            $openId = $res['openid'];
        }
        return ['code'=>1,'msg'=>'请求成功','openid'=>$openId];
    }


    /**
     *
     *
     *Method sendCode
     *user:大飞
     *time:2021/7/2 19:36
     */
    public function sendCode()
    {
        //接收手机号
        $data['phone'] = $this->request->param();
    }


}
